package main

import (
	"crypto/ecdsa"
	"crypto/rand"
	"crypto/sha256"
	"crypto/elliptic"
	"math/big"
	"fmt"
	"time"
)

func hexprint(k []byte){
	len:= len(k)
	for i:=0;i<len;i++{
		fmt.Printf("%02X",k[i])
	}
	fmt.Println()
}

func ecdsa_test() {
	c := elliptic.P256();

	pri, _ := ecdsa.GenerateKey(c, rand.Reader)
	hexprint(pri.D.Bytes())
	hexprint(pri.X.Bytes())
	msg := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	hfunc := sha256.New()
	hfunc.Write(msg)
	hmac := hfunc.Sum(nil)
	var r, s *big.Int
	fmt.Println("ecdsa:")
	fmt.Println("ecdsa sign")
	fmt.Println(pri.Curve.Params().Gx.Bytes())
	t1:=time.Now()
	for i:=0;i<10000;i++{
		r, s, _ = ecdsa.Sign(rand.Reader, pri, hmac)
	}
	//fmt.Println(r)
	//	fmt.Println(s)
	elspsed:=time.Since(t1)
	fmt.Println(elspsed)

	fmt.Println("ecdsa verify")
	t1=time.Now()
	for i:=0;i<10000;i++{
		ecdsa.Verify(&pri.PublicKey,hmac,r,s,)
	}
	elspsed=time.Since(t1)
	fmt.Println(elspsed)
}

func main(){
	ecdsa_test()
}
